﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Web.Caching;
using DocManagerORM.ORM;


namespace DocManagerControls.Controls
{
    public class QueryDoc : QueryBase
    {
        public QueryDoc()
        {

        }


        public IQueryable<vwDocumenti> prelevaDocView(string pDocName, bool pISID, int? pCatId, int? pSottoCatID,
                 int? pTagsID, string pTagsPersonali)
        {
            //if (string.IsNullOrEmpty(pDocName)) return null;

            IQueryable<vwDocumenti> query = m_Entity.vwDocumenti;

            if (pISID) //cerca per ID
            {
                int iddoc = 0;
                if(! int.TryParse(pDocName,out iddoc)) return null;

                return query.Where(x => x.ID == iddoc);   
            }

            if (string.IsNullOrEmpty(pDocName)) 
                query = query.Where(x => x.FileName.StartsWith(pDocName));             
          
            if (pCatId.HasValue)
                query = query.Where(x => x.Fk_IDCat == pCatId.Value);

            if (pSottoCatID.HasValue)
                query = query.Where(x => x.Fk_IDSotCat == pSottoCatID.Value);

            if (pTagsID.HasValue)
                query = query.Where(x => x.Fk_IDTags == pTagsID.Value);

            if (!string.IsNullOrEmpty(pTagsPersonali))
                query = query.Where(x => x.TagsLiberi == pTagsPersonali);

            return query;
        
        }//fM



        public IQueryable<Documenti> prelevaDoc(string pDocName, bool pISID, int? pCatId, int? pSottoCatID,
                 int? pTagsID, string pTagsPersonali)
        {
            if (string.IsNullOrEmpty(pDocName)) return null;

            IQueryable<Documenti> query = m_Entity.Documenti;

            if (pISID) //cerca per ID
            {
                int iddoc = 0;
                if (!int.TryParse(pDocName, out iddoc)) return null;

                return query.Where(x => x.ID == iddoc);
            }
            else
            {
                return query.Where(x => x.FileName.StartsWith(pDocName));
            }


            if (pCatId.HasValue)
                query = query.Where(x => x.Fk_IDCat == pCatId.Value);

            if (pSottoCatID.HasValue)
                query = query.Where(x => x.Fk_IDSotCat == pSottoCatID.Value);

            if (pTagsID.HasValue)
                query = query.Where(x => x.Fk_IDTags == pTagsID.Value);

            if (!string.IsNullOrEmpty(pTagsPersonali))
                query = query.Where(x => x.TagsLiberi == pTagsPersonali);

            return query;

        }//fM



        public IQueryable<Documenti> prelevaDoc(string pDocName, bool pISID)
        {
            if (string.IsNullOrEmpty(pDocName)) return null;

            IQueryable<Documenti> query = m_Entity.Documenti;

            if (pISID) //cerca per ID
            {
                int iddoc = 0;
                if (!int.TryParse(pDocName, out iddoc)) return null;

                return query.Where(x => x.ID == iddoc);
            }
            else
            {
                return query.Where(x => x.FileName.StartsWith(pDocName));
            }

            return query;

        }//fM

        public IQueryable<Documenti> prelevaDoc(int? pDocID)
        {
            IQueryable<Documenti> query = m_Entity.Documenti;

            if (pDocID.HasValue)
                return query.Where(x => x.ID == pDocID.Value);
            else
                return query;

        }//fM

        /// <summary>
        /// Torna tupla : totSalvati, ID
        /// </summary>
       public Tuple<int,int> salvaNuovoDocInDb(string pFilename, byte[] pBytesDoc, string pEstensione, int pCatID,
                  int pSottoCatID, int? pIdTagsPredefiniti, string pTagPersonali,
            Guid pRuoloVediId, Guid pRuoloModId, string pNote, int pOriginalSize, bool pIsZipped, bool pIsEncrypted)
        {

             try
            {
                if (pBytesDoc == null || pBytesDoc.Length == 0) return Tuple.Create<int,int>(0,-1) ;;

            Documenti doc = Documenti.CreateDocumenti(-1, pFilename, pBytesDoc, pEstensione, pCatID);

            doc.Fk_IDSotCat = pSottoCatID;
            doc.Fk_IDTags = pIdTagsPredefiniti.HasValue ? null : pIdTagsPredefiniti;
            doc.TagsLiberi = pTagPersonali;
            doc.Note = pNote;
            doc.IsZipped = pIsZipped;
            doc.IsEncrypted = pIsEncrypted;
            doc.OriginalSize = pOriginalSize;

            m_Entity.AddToDocumenti(doc);
            int tot = m_Entity.SaveChanges( System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

            return Tuple.Create<int,int>(tot,doc.ID) ;

                 }
            catch (Exception ex)
            {
                throw;
            }


        }//fM

    }//fC
}